var entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    BaseFun = require('../../../Base/server/controllers/Function'),
    _ = require('lodash')

//获取系统设置的 ERP服务器地址
exports.getERPSysParamValues = function (req, res) {

    exports.getERPParams(function (err, data) {
        BaseFun.responseFun(res, err, data);
    });

};

//获取系统ERP参数值
exports.getERPParams = function (completeFun) {
    var entity = entityCollection.getEntity("Parameter");
    var paramValues = {};
    var erpHost = 'ERPHostAddress';
    var erpPort = 'ERPPort';
    var erpVDir = 'ERPVirtualDir';

    entity.find({ Code: { $in: [erpHost, erpPort, erpVDir] } }, null, null, function (err, data) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '参数查询错误';
            newErr.message = '通过编码查询【ERP服务器地址】系统参数失败，请检查查询条件';
            completeFun(newErr);
        } else {
            for (var i = 0; i < data.length; i++) {
                var record = data[i]._doc;
                if (record.Code == 'ERPHostAddress') {
                    paramValues.ERPHostAddress = record.Value;
                } else if (record.Code == 'ERPPort') {
                    paramValues.ERPPort = record.Value;
                } else if (record.Code == 'ERPVirtualDir') {
                    paramValues.ERPVirtualDir = record.Value;
                }
            }

            if (paramValues.ERPHostAddress == null || paramValues.ERPHostAddress == 'undefined') {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '参数查询错误';
                newErr.message = '通过编码查询【ERP服务器地址】系统参数失败，请检查查询条件';
                completeFun(newErr);
            }
            //  else if (paramValues.ERPPort == null || paramValues.ERPPort == 'undefined') {
            //     var newErr = new Error();
            //     newErr.leval = 9;
            //     newErr.title = '参数查询错误';
            //     newErr.message = '通过编码查询【ERP服务器端口】系统参数失败，请检查查询条件';
            //     completeFun(newErr);
            // } else if (paramValues.ERPVirtualDir == null || paramValues.ERPVirtualDir == 'undefined') {
            //     var newErr = new Error();
            //     newErr.leval = 9;
            //     newErr.title = '参数查询错误';
            //     newErr.message = '通过编码查询【ERP虚拟目录】系统参数失败，请检查查询条件';
            //     completeFun(newErr);
            // } 
            else {
                completeFun(null, paramValues);
            }
        }
    });

};

exports.getWarehouseList = function (req, res) {

    var parameter = req.body.Parameter
    var contion = {};
    contion.ErpID = { $gt: 0 }

    if (parameter&&parameter.orgCode) {
        contion.OrgCode = parameter.orgCode
    }

    var entity = entityCollection.getEntity("StorageLocation");

    entity.find(contion, null, null, function (err, records) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '存储地点查询错误';
            newErr.message = '获取存储地点列表失败，请检查智能工厂端存储地点设置！';
            BaseFun.responseFun(res, newErr, null);
        } else {
            var whList = [];
            _.forEach(records, function (record) {
                var wh = {};
                wh.Code = record.Code;
                wh.Name = record.Name;
                wh.ErpID = record.ErpID;
                wh.OrgCode = record.OrgCode;
                wh.OrgName = record.OrgName;
                wh.Org = record.Org;

                whList.push(wh);
            });

            BaseFun.responseFun(res, null, whList);
        }
    });
};